#include "sys.h"

CSys::CSys() : m_App(NULL)
			 , m_TSysTask(NULL){

}

CSys::~CSys(){
	if(m_App)
		delete m_App;
}

void CSys::initialize(){
	g_User.load_user_info();
	g_Config.load_config_info();
	m_App = new CApp;
	m_TSysTask = new TSysTask;
	m_App->initialize();
}

void CSys::run(){
	m_App->run();
}

bool CSys::get_app_state(){
	return m_App->is_running();
}

void CSys::suspend(){
	m_App->suspend();
}

void CSys::save(){
	XMLNode xMainNode = XMLNode::createXMLTopNode("xml",TRUE);
	xMainNode.addAttribute("version","1.0");
	xMainNode.addAttribute("encoding","gb2312");
	XMLNode child1 = xMainNode.addChild("SV");
	char  tempbuf[11];
	_tzset();
	_strdate(tempbuf);
	std::string FileName;
	FileName = tempbuf;
	memset(tempbuf,0,20);
	_strtime(tempbuf);
	FileName += " ";
	FileName += tempbuf;
	child1.addAttribute("Time", FileName.c_str());
	XMLNode child;
	for(TSysTaskIter iter = m_TSysTask->begin(); iter != m_TSysTask->end(); ++iter){
		if((*iter).m_Type){
			child = child1.addChild("Dir");
		}
		else{
			child = child1.addChild("File");
		}
		if((*iter).m_Action){
			child.addAttribute("Action","Del");
		}
		else{
			child.addAttribute("Action","Add");
		}
		string size = UlToStr((*iter).m_Size);
		child.addAttribute("Size", size.c_str());
	}
	time_t ltime;
	time( &ltime);
	memset(tempbuf,0,20);
	_i64toa(ltime,tempbuf,10);
	xMainNode.writeToFile(tempbuf);
	m_App->newTask(tempbuf);
}

void CSys::add_file(char * n_fileName, unsigned long n_fileSize){
	m_SSysTask.m_Action = 0;
	m_SSysTask.m_Type = 0;
	m_SSysTask.m_Name = n_fileName;
	m_SSysTask.m_Size = n_fileSize;
	m_TSysTask->push_back(m_SSysTask);
}

void CSys::add_dir(char * n_dirName, unsigned long n_dirSize){
	m_SSysTask.m_Action = 0;
	m_SSysTask.m_Type = 1;
	m_SSysTask.m_Name = n_dirName;
	m_SSysTask.m_Size = n_dirSize;
	m_TSysTask->push_back(m_SSysTask);
}

void CSys::del_file(char * n_fileName){
	m_SSysTask.m_Action = 1;
	m_SSysTask.m_Type = 0;
	m_SSysTask.m_Name = n_fileName;
	m_SSysTask.m_Size = 0;
	m_TSysTask->push_back(m_SSysTask);
}

void CSys::del_dir(char * n_dirName){
	m_SSysTask.m_Action = 1;
	m_SSysTask.m_Type = 1;
	m_SSysTask.m_Name = n_dirName;
	m_SSysTask.m_Size = 0;
	m_TSysTask->push_back(m_SSysTask);
}
